package com.ruoyi.system.service.complex;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class TravelPlanner {

    static class POI {
        String name;
        double latitude;
        double longitude;
        int priority; // 优先级
        int visitDuration; // 游玩时长，单位:分钟

        POI(String name, double latitude, double longitude, int priority, int visitDuration) {
            this.name = name;
            this.latitude = latitude;
            this.longitude = longitude;
            this.priority = priority;
            this.visitDuration = visitDuration;
        }
    }

    public static double haversine(String lat1, String lon1, String lat2, String lon2) {
        return TravelPlanner.haversine(Double.parseDouble(lat1), Double.parseDouble(lon1), Double.parseDouble(lat2), Double.parseDouble(lon2));
    }

    // 计算两点之间的哈弗赛因距离
    public static double haversine(double lat1, double lon1, double lat2, double lon2) {
        final double R = 6371; // 地球半径，单位:公里
        double deltaLat = Math.toRadians(lat2 - lat1);
        double deltaLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(
                deltaLon / 2) * Math.sin(deltaLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return R * c; // 返回距离，单位:公里
    }

    // 假设这里是调用第三方地图服务返回的时间
    public static int getTravelTime(double lat1, double lon1, double lat2, double lon2) {
        // 模拟返回的行驶时间，单位:分钟
        return (int) (TravelPlanner.haversine(lat1, lon1, lat2, lon2) * 10); // 简单模拟
    }

    // 智能规划行程
    public static List<POI> planTrip(List<POI> pois, double startLat, double startLon, int totalTime) {
        List<POI> itinerary = new ArrayList<>();
        List<POI> sortedPOIs = new ArrayList<>(pois);
        sortedPOIs.sort((a, b) -> Integer.compare(b.priority, a.priority)); // 根据优先级排序

        double currentLat = startLat;
        double currentLon = startLon;
        int remainingTime = totalTime;

        for (POI poi : sortedPOIs) {
            int travelTime = TravelPlanner.getTravelTime(currentLat, currentLon, poi.latitude, poi.longitude);
            if (travelTime + poi.visitDuration <= remainingTime) {
                itinerary.add(poi);
                remainingTime -= (travelTime + poi.visitDuration);
                currentLat = poi.latitude;
                currentLon = poi.longitude;
            }
        }

        return itinerary;
    }

    public static void main(String[] args) {
        List<POI> pois = Arrays.asList(new POI("景点A", 39.9042, 116.4074, 10, 60), new POI("景点B", 34.0522, - 118.2437, 5, 120),
                new POI("景点C", 37.7749, - 122.4194, 7, 90));

        double startLat = 39.9042;
        double startLon = 116.4074;
        int totalTime = 360; // 总时间，单位:分钟

        List<POI> optimizedTrip = TravelPlanner.planTrip(pois, startLat, startLon, totalTime);

        System.out.println("优化行程:");
        for (POI poi : optimizedTrip) {
            System.out.println(poi.name);
        }
    }
}

